<template>
  <div class="login-form-wrapper">
    <div class="login-form-title">{{
      isRegister ? $t('register.form.title') : $t('login.form.title')
    }}</div>
    <div class="login-form-sub-title">{{
      isRegister ? $t('register.form.title') : $t('login.form.title')
    }}</div>
    <div class="login-form-error-msg">{{ errorMessage }}</div>
    <a-form
      v-if="!isRegister"
      ref="loginForm"
      :model="loginUserInfo"
      class="login-form"
      layout="vertical"
      @submit="handleLogin"
    >
      <a-form-item
        field="name"
        :rules="[{ required: true, message: $t('login.form.userName.errMsg') }]"
        :validate-trigger="['change', 'blur']"
        hide-label
      >
        <a-input
          v-model="loginUserInfo.name"
          :placeholder="$t('login.form.userName.placeholder')"
        >
          <template #prefix>
            <icon-user />
          </template>
        </a-input>
      </a-form-item>
      <a-form-item
        field="password"
        :rules="[{ required: true, message: $t('login.form.password.errMsg') }]"
        :validate-trigger="['change', 'blur']"
        hide-label
      >
        <a-input-password
          v-model="loginUserInfo.password"
          :placeholder="$t('login.form.password.placeholder')"
          allow-clear
        >
          <template #prefix>
            <icon-lock />
          </template>
        </a-input-password>
      </a-form-item>
      <a-space :size="16" direction="vertical">
        <a-button type="primary" html-type="submit" long :loading="loading">
          {{ $t('login.form.login') }}
        </a-button>
        <a-button
          type="text"
          long
          class="login-form-register-btn"
          @click="isRegister = true"
        >
          {{ $t('login.form.register') }}
        </a-button>
      </a-space>
    </a-form>

    <a-form
      v-else
      ref="registerForm"
      :model="registerUserInfo"
      class="login-form"
      layout="vertical"
      @submit="handleRegister"
    >
      <a-form-item
        field="name"
        :rules="[{ required: true, message: $t('login.form.userName.errMsg') }]"
        :validate-trigger="['change', 'blur']"
        hide-label
      >
        <a-input
          v-model="registerUserInfo.name"
          :placeholder="$t('login.form.userName.placeholder')"
        >
          <template #prefix>
            <icon-user />
          </template>
        </a-input>
      </a-form-item>
      <a-form-item
        field="password"
        :rules="[{ required: true, message: $t('login.form.password.errMsg') }]"
        :validate-trigger="['change', 'blur']"
        hide-label
      >
        <a-input-password
          v-model="registerUserInfo.password"
          :placeholder="$t('login.form.password.placeholder')"
          allow-clear
        >
          <template #prefix>
            <icon-lock />
          </template>
        </a-input-password>
      </a-form-item>
      <a-space :size="16" direction="vertical">
        <a-button type="primary" long :loading="loading" html-type="submit">
          {{ $t('login.form.register') }}
        </a-button>
        <a-button
          type="text"
          @click="isRegister = false"
          class="login-form-register-btn"
          long
        >
          {{ $t('login.form.login') }}
        </a-button>
      </a-space>
    </a-form>
  </div>
</template>

<script lang="ts" setup>
  import { ref, reactive } from 'vue';
  import { useRouter } from 'vue-router';
  import { Message } from '@arco-design/web-vue';
  import { ValidatedError } from '@arco-design/web-vue/es/form/interface';
  import { useI18n } from 'vue-i18n';
  import { useStorage } from '@vueuse/core';
  import { useUserStore } from '@/store';
  import useLoading from '@/hooks/loading';
  import { register, type LoginData } from '@/api/user';
import { setToken } from '@/utils/auth';

  const isRegister = ref(false);

  const router = useRouter();
  const { t } = useI18n();
  const errorMessage = ref('');
  const { loading, setLoading } = useLoading(false);
  const userStore = useUserStore();

  const loginUserInfo = reactive({
    name: '',
    password: '',
  });

  const registerUserInfo = reactive({
    name: '',
    password: '',
  });

  const handleRegister = async ({ values, errors }) => {
    try {
      loading.value = true;
      const res = await register(values);
      if (res.code === 200) {
        Message.success('注册成功');
        isRegister.value = false;
      }
    } catch (error) {
      Message.error(error);
    } finally {
      loading.value = false;
    }
  };

  const handleLogin = async ({ values, errors }) => {
    try {
      loading.value = true;
      const res = await userStore.login(values);
      
      if (res.code === 200) {
        Message.success('登陆成功');
        setToken(res?.data as string)
        router.push('/dashboard');
      }
    } catch (error) {
      Message.error(error);
    } finally {
      loading.value = false;
    }
  };
</script>

<style lang="less" scoped>
  .login-form {
    &-wrapper {
      width: 320px;
    }

    &-title {
      color: var(--color-text-1);
      font-weight: 500;
      font-size: 24px;
      line-height: 32px;
    }

    &-sub-title {
      color: var(--color-text-3);
      font-size: 16px;
      line-height: 24px;
    }

    &-error-msg {
      height: 32px;
      color: rgb(var(--red-6));
      line-height: 32px;
    }

    &-password-actions {
      display: flex;
      justify-content: space-between;
    }

    &-register-btn {
      color: var(--color-text-3) !important;
    }
  }
</style>
